package semantic_compute.entity_linking.yago_linker;

import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import common.dao.orient.OrientGraphDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import semantic_compute.common.yago_helper.YagoLuceneQueryer;
import semantic_compute.common.yago_helper.YagoQueryHelper;
import semantic_compute.common.yago_schema.MatchedVertexItem;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by julianzliu on 4/21/2017.
 */
public class YagoSimpleEntityLinker {

    static final Logger logger = LoggerFactory.getLogger(YagoSimpleEntityLinker.class);

    private OrientGraphDao orientGraphDao;
    private OrientGraph orientGraph;

    private YagoLuceneQueryer luceneQueryer;

    public YagoSimpleEntityLinker(){
        this.orientGraphDao = new OrientGraphDao();
        this.orientGraph = this.orientGraphDao.getGraph();

        this.luceneQueryer = new YagoLuceneQueryer();
    }


    /***********************************************************
     *
     * @param phrase
     * 返回文本匹配的类 （rid, yago_id, indegree, {label_set} ）
     */
    public List<MatchedVertexItem> getIndgreeSortedResult(String phrase){
        String query = YagoQueryHelper.phraseToQuery(phrase);
        logger.info("[getIndgreeSortedResult] query_string = {}", query);
        String sql = String.format(
                "select @rid as rid, yago_id as yid, inE().asList().size() as in_size ,outE().asList().size() as out_size, out() from (" + "\n"
                        + "  select from ( " + "\n"
                        + "    select expand(in()) from (" + "\n"
                        + "      select *"   + "\n"
                        + "         from YAGO_LABEL_CLASS"     + "\n"
                        + "         where yago_label LUCENE \"%s\"" + "\n"
                        + "       )" + "\n"
                        + "   )" + "\n"
                        + "    where yago_type = 'OTHER' " + "\n"
                        + " )" + "\n"
                ,query
        );
        logger.info("[getIndgreeSortedResult] sql = \n{}" , sql);
        Iterable<Vertex> vertices = (Iterable<Vertex>) this.orientGraph.command(
                new OCommandSQL(sql )
        ).execute();


        List<MatchedVertexItem> matchedVertexItems = new ArrayList<>();
        //Map<String, Integer>  yago_concept_indegree = new HashMap<>();
        for(Vertex row: vertices){
            MatchedVertexItem matchedVertexItem = new MatchedVertexItem();
            Integer in_size = row.getProperty("in_size");
            Integer out_size = row.getProperty("out_size");
            String yago_id = row.getProperty("yid");
            String rid = YagoQueryHelper.getRidOfVertex(  (Vertex)row.getProperty("rid")  );
            matchedVertexItem.yago_id = yago_id;
            matchedVertexItem.rid = rid;
            matchedVertexItem.indegree = in_size;
            matchedVertexItem.outdegree = out_size;
            System.out.println("in_size=" + in_size + " , out_size=" + out_size + " , yago_id = " + yago_id + " , rid = " + rid);
            Iterable<Vertex> out_vertexs = row.getProperty("out");
            for(Vertex out_vertex: out_vertexs){
                matchedVertexItem.out_rids.add( YagoQueryHelper.getRidOfVertex(out_vertex) );
            }
            matchedVertexItems.add(matchedVertexItem);
            //yago_concept_indegree.put(yago_id, in_size);
        }
        //return yago_concept_indegree;
        this.luceneQueryer.getLuceneSortedResultForEntity( phrase );
        return matchedVertexItems;
    }


    /***********************************************************
     *
     */
    public static void main(String[] args){
        String phrase = "Hawaii";
        if(args.length > 0){
            phrase = "";
            for(String token:args) phrase += " " + token;
        }
        YagoSimpleEntityLinker entityLinker = new YagoSimpleEntityLinker();
        List<MatchedVertexItem> vertexItems = entityLinker.getIndgreeSortedResult( phrase );

    }


}
